<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form label-width="100px" size="small">
          <el-form-item label="企业名称：" prop="name">
            <el-input v-model="params.name" placeholder="请输入企业名称" style="width:300px;margin:0 10px 0 0" />
            <el-button type="primary" @click="onClick">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button
        type="primary"
        size="small"
        style="margin: 0 0 5px 0"
        @click="$router.push('/enterprise/addEnt')"
      >
        添加企业
      </el-button>
      <div>
        <el-table
          :data="tableData"
          style="width: 100%;margin:15px 10px"
          @expand-change="onExpand"
        >
          <!-- 下拉表 -->
          <el-table-column type="expand">
            <template>
              <el-table style="width: 100%;margin:15px 10px" :data="expandList">
                <el-table-column label="租赁楼宇" prop="buildingName" />
                <el-table-column label="租赁起止时间" prop="startTime" />
                <el-table-column label="合同状态" prop="status">
                  <template #default="{row}">
                    <el-tag v-if="row.status===0" plain size="mini">待生效</el-tag>
                    <el-tag v-else-if="row.status===1" type="success" plain size="mini">生效中</el-tag>
                    <el-tag v-else-if="row.status===2" type="danger" plain size="mini">已到期</el-tag>
                    <el-tag v-else-if="row.status===3" type="info" plain size="mini">已退租</el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="操作">
                  <template #default="{row}">
                    <el-button
                      type="text"
                      size="small"
                      :disabled="row.status===0"
                    >
                      续租
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                    >
                      退租
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      :disabled="row.status===0||row.status===1"
                    >
                      删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <!-- 表头 -->
          <el-table-column
            type="index"
            width="110"
            label="序号"
            :index="indexMethod"
          />
          <el-table-column
            label="企业名称"
            prop="name"
          />
          <el-table-column
            label="联系人"
            prop="contact"
          />
          <el-table-column
            label="联系人电话"
            prop="contactNumber"
          />
          <el-table-column label="操作">
            <template>
              <el-button
                type="text"
                size="small"
              >
                添加合同
              </el-button>
              <el-button
                type="text"
                size="small"
              >
                查看
              </el-button>
              <el-button
                type="text"
                size="small"
              >
                编辑
              </el-button>
              <el-button
                type="text"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getListEtpAPI, getListExpandAPI } from '@/api/enterprise'
export default {
  // 封装页面接口API 定义初始数据 封装一个列表渲染接口方法方便复用 一进页面就调用这个接口
  data() {
    return {
      params: {
        id: '',
        name: '',
        contact: '',
        contactNumber: '',
        demoFlag: null
      },
      tableData: [],
      expandList: [],
      // 总页数
      total: 1
    }
  },
  created() {
    this.onLoad()
  },
  methods: {
    // 点击查询
    onClick() {
      this.onLoad()
    },
    // 点击下拉 发请求 拿取数据渲染页面 根据每行id发请求
    async onExpand(row, expandedRows) {
      if (expandedRows.length > 0) {
        const res = await getListExpandAPI(row.id)
        // console.log(res.data)
        this.expandList = res.data
      }
    },
    // 封装列表展示数据
    async onLoad() {
      const res = await getListEtpAPI(this.params)
      // console.log(res.data.rows)
      this.tableData = res.data.rows
      // console.log(res.data.rows[0].id)
    },
    //
    indexMethod(index) {
      return index + 1
    }
  }
}
</script>

<style>
.demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
